package com.google.gwt.sample.stockwatche.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StockWatcher implements EntryPoint {

  private static final int REFRESH_INTERVAL = 5000;
private VerticalPanel mainPanel = new VerticalPanel();
  private FlexTable stocksFlexTable = new FlexTable();
  private HorizontalPanel addPanel = new HorizontalPanel();
  private TextBox newSymbolTextBox = new TextBox();
  private Button addStockButton = new Button("Add");
  private Label lastUpdatedLabel = new Label();
  private ArrayList<String> stocks = new ArrayList<String>();

  /**
   * Entry point method.
   */
  public void onModuleLoad() {
	  stocksFlexTable.setText(0, 0, "Symbol");
	  stocksFlexTable.setText(0, 1, "Price");
	  stocksFlexTable.setText(0, 2, "Change");
	  stocksFlexTable.setText(0, 3, "Remove");
	  
	  addPanel.add(newSymbolTextBox);
	  addPanel.add(addStockButton);
	  
	  mainPanel.add(stocksFlexTable);
	  mainPanel.add(addPanel);
	  mainPanel.add(lastUpdatedLabel);
	  
	  RootPanel.get("stockList").add(mainPanel);
	
	  newSymbolTextBox.setFocus(true);
	  
	  // Event-Handling
	  addStockButton.addClickHandler(new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
			// TODO Auto-generated method stub
			addStock();
		}		
	});
	  
	  newSymbolTextBox.addKeyPressHandler(new KeyPressHandler() {
		
		@Override
		public void onKeyPress(KeyPressEvent event) {
			// Only when ENTER is pressed
			if (event.getCharCode() == KeyCodes.KEY_ENTER) {
				addStock();
			}
			
		}
	});
	  
	  // Refreshing every 5 seconds
	    // Move cursor focus to the text box.
	    newSymbolTextBox.setFocus(true);

	    // Setup timer to refresh list automatically.
	    Timer refreshTimer = new Timer() {
	      @Override
	      public void run() {
	        refreshWatchList();
	      }
	    };
	    refreshTimer.scheduleRepeating(REFRESH_INTERVAL);

  }
  
  protected void refreshWatchList() {
	// TODO Auto-generated method stub
	
}

/*
   * Adds a new stock to the table.
   */
  private void addStock() {
	  final String symbol = newSymbolTextBox.getText().toUpperCase().trim();
	    newSymbolTextBox.setFocus(true);

	    // Stock code must be between 1 and 10 chars that are numbers, letters, or dots.
	    if (!symbol.matches("^[0-9A-Z\\.]{1,10}$")) {
	      Window.alert("'" + symbol + "' is not a valid symbol.");
	      newSymbolTextBox.selectAll();
	      return;
	    }

	    newSymbolTextBox.setText("");

	    // Don't add the stock if it's already in the table.
	    if(stocks.contains(symbol)){
	    	return;
	    }	    

	    // Add the stock to the table.
	    
	    int row = stocksFlexTable.getRowCount();
	    stocks.add(symbol);
	    stocksFlexTable.setText(row, 0, symbol);

	    // Add a button to remove this stock from the table.
	    
	    Button removeStockButton = new Button("X");
	    stocksFlexTable.setWidget(row, 3, removeStockButton);
	    removeStockButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				int rowToRemove = stocks.indexOf(symbol);
				stocksFlexTable.removeRow(rowToRemove+1);
				stocks.remove(rowToRemove);
			}
		});

	    // Get the stock price.	    
	    refreshWatchList();


	}

}